/*
 * Copyright (c) 2010, VeRSI Consortium
 *   (Victorian eResearch Strategic Initiative, Australia)
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the VeRSI, the VeRSI Consortium members, nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package versi.vitro.oaiserviceprovider.service.handle.impl;

import org.apache.log4j.Logger;

import versi.vitro.oaiserviceprovider.service.handle.api.HandleService;
import versi.vitro.oaiserviceprovider.service.handle.helper.HandleHelper;
import versi.vitro.oaiserviceprovider.service.handle.helper.HandleHelperImpl;

/**
import au.net.pilin.jahdl.service.core.dto.PilinHandleRequest;
import au.net.pilin.jahdl.service.core.exceptions.HandleCreateException;
import au.net.pilin.jahdl.service.core.exceptions.HandlePrivateKeyException;
import au.net.pilin.jahdl.service.core.exceptions.HandleRequestDataException;
import au.net.pilin.jahdl.service.core.impl.HandleCreateServiceImpl;
import au.net.pilin.jahdl.util.core.PilinHandleType;
*/

/**
 * Service implementing {@link HandleService}.
 */
public class HandleServiceImpl implements HandleService {

    /** log4j instance. */
    private static final Logger LOGGER = Logger.getLogger(HandleServiceImpl.class);

    /** handle namespace suffix. */
    private String handleNamespace;

    /** handle admin server key. */
    private String handleServerKey;

    /** handle admin pass phrase. */
    private String keyPassphrase;

    /** Handle Service Helper. */
    private HandleHelper handleHelper;

    /**
     * Constructor.
     * 
     * @param handleHelper
     *            Handle service helper.
     */
    public HandleServiceImpl(HandleHelper handleHelper) {
	super();
	this.handleHelper = handleHelper;
    }

    /**
     * Default Constructor.
     * 
     */
    public HandleServiceImpl() {
	super();
	this.handleHelper = new HandleHelperImpl();
    }

    /** {@inheritDoc} */
    public void setHandleNamespace(final String handleNamespace) {
	this.handleNamespace = handleNamespace;
    }

    /** {@inheritDoc} */
    public void setHandleServerKey(final String handleServerKey) {
	this.handleServerKey = handleServerKey;
    }

    /** {@inheritDoc} */
    public void setKeyPassphrase(final String keyPassphrase) {
	this.keyPassphrase = keyPassphrase;
    }

    /** {@inheritDoc} */
    public void configHandle(final String handleNamespace, final String handleServerKey, final String keyPassphrase) {

	setHandleNamespace(handleNamespace);
	setHandleServerKey(handleServerKey);
	setKeyPassphrase(keyPassphrase);

    }

    /** {@inheritDoc} */
    /*public String createHandleforURL(final String url) {

	String urlHdl = null;

	try {
	    PilinHandleRequest handleRequest = createHandleRequest();

	    String handleSuffix = null;
	    handleRequest.setHandleSuffix(handleSuffix);
	    // String url =
	    // generateViewDatasetURL(PropertyFileReader.getInstance().getValue(PropertyFileReader.HANDLE_SERVER_NAMESPACE)
	    // + "/"
	    // + handleSuffix, relPath);

	    handleRequest.setHandleValueWithDefaultPermissions(PilinHandleType.URL, url);

	    HandleCreateServiceImpl hcs = new HandleCreateServiceImpl();

	    urlHdl = hcs.createHandle(handleRequest);

	} catch (HandleCreateException hce) {

	    LOGGER.error("Unable to create a handle", hce);
	    throw new RuntimeException("Unable to create a handle.", hce);

	} catch (HandleRequestDataException hrde) {

	    LOGGER.error("Error in requested data of handle creation. ", hrde);
	    throw new RuntimeException("Unable to create a handle", hrde);

	} catch (HandlePrivateKeyException hpke) {

	    LOGGER.error("Error in private key of handle creation. ", hpke);
	    throw new RuntimeException("Unable to create a handle", hpke);
	}

	return urlHdl;

    }*/

    /** Creates a handle creation request. */
   /* private PilinHandleRequest createHandleRequest() {

	PilinHandleRequest handleRequest = new PilinHandleRequest();

	try {

	    handleRequest.setHandleNamespace(handleNamespace);
	    handleRequest.setHandleServerKey(handleServerKey);
	    handleRequest.setKeyPassphrase(keyPassphrase);

	} catch (HandleRequestDataException hrde) {

	    LOGGER.error("Error in requested data of handle creation. ", hrde);
	    throw new RuntimeException("Unable to create a handle", hrde);
	}
	return handleRequest;

    }*/

    /** {@inheritDoc} */
    public HandleHelper getHandleHelper() {
	return handleHelper;
    }

    /** {@inheritDoc} */
    public void configureANDSHandleService(final String url, final String AppId, final String identifier,
	    final String authDomain) {

	handleHelper.setAndServiceURL(url);
	handleHelper.setServiceAppId(AppId);
	handleHelper.setAuthDomain(authDomain);
	handleHelper.setIdentifier(identifier);

    }

    /** {@inheritDoc} */
    public String pidCreateHandle(final String resourceType, final String resourceValue) {

	return handleHelper.pidCreateHandle(resourceType, resourceValue);
    }

    /** {@inheritDoc} */
    public String pidCreateHandle(final String resourceType, final String resourceValue, final int index) {

	return handleHelper.pidCreateHandle(resourceType, resourceValue, index);
    }

    /** {@inheritDoc} */
    public boolean pidDeleteHandleValue(final String handle, final int index) {

	return handleHelper.pidDeleteValueByIndex(handle, index);
    }

    /** {@inheritDoc} */
    public boolean pidModifyHandleValue(final String handle, final int index, final String resourceValue) {

	return handleHelper.pidModifyHandle(handle, index, resourceValue);
    }

    /** {@inheritDoc} */
    public String pidAddHandleValue(final String handle, final String resourceType, final String resourceValue) {

	return handleHelper.pidAddValue(handle, resourceType, resourceValue);
    }

    /** {@inheritDoc} */
    public String pidAddHandleValue(final String handle, final String resourceType, final String resourceValue,
	    final int index) {

	return handleHelper.pidAddValue(handle, resourceType, resourceValue, index);
    }

    /** {@inheritDoc} */
    public String[] pidListHandle() {

	return handleHelper.pidListHandle();
    }
}
